package com.eduService.teacher.service.impl;


import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eduService.teacher.entity.EduSubject;
import com.eduService.teacher.entity.excelSubject;
import com.eduService.teacher.listener.SubjectListener;
import com.eduService.teacher.mapper.EduSubjectMapper;
import com.eduService.teacher.service.IEduSubjectService;
import com.eduService.teacher.vo.subject.FirstSubject;
import com.eduService.teacher.vo.subject.SecondSubject;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author Mr.Tang
 * @since 2022-04-29
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements IEduSubjectService {

    @Override
    public void readSubject(MultipartFile file, IEduSubjectService subjectService) {
        try {
            System.out.println(1);
            InputStream in = file.getInputStream();
            EasyExcel.read(in, excelSubject.class, new SubjectListener(subjectService)).sheet().doRead();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<FirstSubject> showCourse() {
//        遍历一级分类数据
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id", "0");
        List<EduSubject> firstList = this.list(wrapper);

//   遍历二级分类数据
        QueryWrapper<EduSubject> wrapper2 = new QueryWrapper<>();
        wrapper2.ne("parent_id", "0");
        List<EduSubject> secondList = this.list(wrapper2);

        List<FirstSubject> Subject = new ArrayList<>();

//     遍历数据，封装到对象当中
        for (EduSubject one : firstList) {
            FirstSubject fSubject = new FirstSubject();
            List<SecondSubject> secondSubjects = new ArrayList<>();
            fSubject.setId(one.getId());
            fSubject.setTitle(one.getTitle());
            for (EduSubject two : secondList) {
                if (two.getParentId().equals(one.getId())) {
                    SecondSubject ssubject = new SecondSubject();
                    ssubject.setId(two.getId());
                    ssubject.setTitle(two.getTitle());
                    secondSubjects.add(ssubject);
                    fSubject.setSecondSubjects(secondSubjects);
                }
            }
            Subject.add(fSubject);
        }

        return Subject;
    }
}
